<template>
  <div class="pdf-pox">

    <!--页码-->

    <!--    <div class="pdf-item">-->
    <!--      <div class="pdf">-->
    <!--        <van-icon name="close" class="i-close" @click="pdfClose" color="black" size="28" v-if="close"/>-->
    <!--        <div class="pagesize">-->
    <!--          &lt;!&ndash;上一页&ndash;&gt;-->
    <!--          <span @click="changePdfPage(0)" class="turn" :class="{grey: numPages==1}">上页</span>-->
    <!--          {{ numPages }} / {{ pageCount }}-->
    <!--          &lt;!&ndash;下一页&ndash;&gt;-->
    <!--          <span @click="changePdfPage(1)" class="turn" :class="{grey: numPages==pageCount}">下页</span>-->
    <!--        </div>-->
    <!--      </div>-->

    <!--      <pdf style="flex: 1"-->
    <!--        :src="pdfSrc"-->
    <!--        :page="currentPage"-->
    <!--        @num-pages="pageCount=$event"-->
    <!--        @page-loaded="currentPage=$event"-->
    <!--        @loaded="loadPdfHandler">-->
    <!--      </pdf>-->

    <!--    </div>-->
    <!--    <pdf-->
    <!--      v-for="i in numPages"-->
    <!--      :key="i"-->
    <!--      :page="i"-->
    <!--      :src="src" style="width: 100%; height: auto;" @num-pages="pageCount=$event">-->
    <!--    </pdf>-->
    <div id="pdf"></div>
  </div>
</template>

<script>
import pdf from 'vue-pdf'


import Pdfh5 from 'pdfh5'

export default {
  name: 'PdfComponent',
  props: {
    pdfSrc: {
      type: String,
      default: ''
    },
    close: {
      type: Boolean,
      default: false
    }
  },
  components: {
    pdf
  },
  data() {
    return {
      numPages: 0, // pdf文件页码
      pageCount: 0, // pdf文件总页数
      src: '' // pdf文件地址
    }
  },
  mounted() {
    this.pdfh5 = new Pdfh5('#pdf', {
      pdfurl: this.pdfSrc
    })
    //监听完成事件
    this.pdfh5.on('complete', function(status, msg, time) {
      console.log('状态：' + status + '，信息：' + msg + '，耗时：' + time + '毫秒，总页数：' + this.totalNum)
    })

    // this.loadPdfHandler()
  },
  methods: {
    async loadPdfHandler() {
      this.$toast.loading()
      this.src = pdf.createLoadingTask(this.pdfSrc)
      this.src.promise.then(pdf => {
        this.numPages = pdf.numPages
      })
    },
    // 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
    changePdfPage(val) {
      // console.log(val)
      if (val === 0 && this.currentPage > 1) {
        this.currentPage--
        // console.log(this.currentPage)
      }
      if (val === 1 && this.currentPage < this.pageCount) {
        this.currentPage++
        // console.log(this.currentPage)
      }
    },
    // pdf加载时
    // loadPdf(e) {
    //   this.currentPage = 1 // 加载的时候先加载第一页
    // },
    pdfClose() {
      this.$emit('pdfClose')
    }
  }
}
</script>

<style lang="scss">

.pdf-pox {
  //position: relative;
  height: calc(100vh - 60px);

  .pdf-item {
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-top: 10px;
    //position: absolute;
    // top: 50px;
    width: 100%;
    background: #ffffff;
    font-size: 16px;
    text-align: center;

    .pdf {
      position: relative;
      height: 30px;

      .i-close {
        position: absolute;
        top: 0px;
        right: 10px;
      }
    }

    .turn {
      color: #017bd3;
      font-weight: bold;
      margin: 0 15px;
    }

    .grey {
      color: gray;
    }
  }
}
</style>
